var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
    data: {
        address: {
            id: 0,
            province_id: 0,
            city_id: 0,
            district_id: 0,
            address: '',
            region: [],
            name: '',
            mobile: '',
            is_default: 0
        },
        addressId: 0,
        openSelectRegion: false,
        region: [],
    },
    mobilechange(e) {
        let mobile = e.detail.value;
        let address = this.data.address;
        if (util.testMobile(mobile)) {
            address.mobile = mobile;
            this.setData({
                address: address
            });
        }
    },
    bindinputName(event) {
        let address = this.data.address;
        address.name = event.detail.value;
        this.setData({
            address: address
        });
    },
    bindinputAddress(event) {
        let address = this.data.address;
        address.address = event.detail.value;
        this.setData({
            address: address
        });
    },
    switchChange(e) {
        let status = e.detail.value;
        let is_default = 0;
        if (status == true) {
            is_default = 1;
        }
        let address = 'address.is_default';
        this.setData({
            [address]: is_default
        });
    },
    getAddressDetail() {
        let that = this;
        let token = wx.getStorageSync('token');
        util.request(api.AddressDetail, {
            id: that.data.addressId,
            token: token
        }, 'POST').then(function(res) {
            if (res.code == 0) {
                that.setData({
                    address: res.data,
                    region: res.data.region
                });
            }
        });
    },
    deleteAddress: function() {
        let id = this.data.addressId;
        wx.showModal({
            title: '提示',
            content: '您确定要删除么？',
            success: function(res) {
                if (res.confirm) {
                    let token = wx.getStorageSync('token');
                    util.request(api.DeleteAddress, {
                        id: id,
                        token: token
                    }, 'POST').then(function(res) {
                        if (res.code == 0) {
                            wx.removeStorageSync('addressId');
                            util.showErrorToast('删除成功');
                            wx.navigateBack();
                        } else {
                            util.showErrorToast(res.msg);
                        }
                    });
                }
            }
        })
    },
    onLoad: function(options) {
        // 页面初始化 options为页面跳转所带来的参数
        if (options.id) {
            this.setData({
                addressId: options.id
            });
            this.getAddressDetail();
        }
    },
    onReady: function() {

    },
    saveAddress() {
        let address = this.data.address;
        if (address.name == '' || address.name == undefined) {
            util.showErrorToast('请输入姓名');
            return false;
        }
        if (address.mobile == '' || address.mobile == undefined) {
            util.showErrorToast('请输入手机号码');
            return false;
        }
        if (address.district_id == 0 || address.district_id == undefined) {
            util.showErrorToast('请输入省市区');
            return false;
        }
        if (address.address == '' || address.address == undefined) {
            util.showErrorToast('请输入详细地址');
            return false;
        }
        let that = this;
        let token = wx.getStorageSync('token');
        util.request(api.SaveAddress, {
            token: token,
            id: address.id,
            name: address.name,
            mobile: address.mobile,
            province_id: address.province_id,
            city_id: address.city_id,
            district_id: address.district_id,
            address: address.address,
            region: address.region,
            is_default: address.is_default,
        }, 'POST').then(function(res) {
            if (res.code === 0) {
                util.showErrorToast(res.msg);
                wx.navigateBack()
            } else {
                util.showErrorToast(res.msg);
            }
        });
    },
    bindRegionChange: function (e) {
        let districtId = 'address.district_id';
        let cityId = 'address.city_id';
        let provinceId = 'address.province_id';
        let region = 'address.region';
        this.setData({
            region: e.detail.value,
            [region]: e.detail.value,
            [provinceId]: e.detail.code[0],
            [cityId]: e.detail.code[1],
            [districtId]: e.detail.code[2],

        })
    },
    onShow: function() {
        let id = this.data.addressId;
        if (id > 0) {
            wx.setNavigationBarTitle({
                title: '编辑地址',
            })
        } else {
            wx.setNavigationBarTitle({
                title: '新增地址',
            })
        }
    },
    onHide: function() {
        // 页面隐藏

    },
    onUnload: function() {
        // 页面关闭

    },
})